package com.book.stat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.entity.QueryRequest;
import com.book.stat.entity.BreakageStat;
import com.book.stat.mapper.BreakageStatMapper;
import com.book.stat.service.BreakageStatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BreakageStatServiceImpl extends ServiceImpl<BreakageStatMapper, BreakageStat> implements BreakageStatService {
    @Autowired
    BreakageStatMapper breakageStatMapper;

    @Override
    public List<BreakageStat> statList(BreakageStat breakageStat) {
        QueryWrapper<BreakageStat> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(breakageStat.getYear())) {
            queryWrapper.eq("year", breakageStat.getYear());
        }
        if (StringUtils.isNotEmpty(breakageStat.getOmit())) {
            queryWrapper.eq("omit", breakageStat.getOmit());
        }
        if (StringUtils.isNotEmpty(breakageStat.getBreakageid())) {
            queryWrapper.eq("breakageid", breakageStat.getBreakageid());
        }
        return this.breakageStatMapper.selectList(queryWrapper);
    }

    @Override
    public void addCount(BreakageStat breakageStat) {
        this.save(breakageStat);
    }

    @Override
    public IPage<BreakageStat> statIpPage(QueryRequest request, BreakageStat breakageStat) {
        QueryWrapper<BreakageStat> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(breakageStat.getBookname())) {
            queryWrapper.eq("bookname", breakageStat.getBookname());
        }
        if (StringUtils.isNotEmpty(breakageStat.getBooktype())) {
            queryWrapper.eq("booktype", breakageStat.getBooktype());
        }
        if (StringUtils.isNotEmpty(breakageStat.getBookfrom())) {
            queryWrapper.eq("bookfrom", breakageStat.getBookfrom());
        }
        Page<BreakageStat> breakageStatPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(breakageStatPage, queryWrapper);
    }
}
